package com.anosine.gpsl.android.ui.activity;

import java.net.URLEncoder;

import com.anosine.gpsl.android.GPSLApp;
import com.anosine.gpsl.android.R;
import com.anosine.gpsl.android.base.BaseActivity;
import com.anosine.gpsl.android.entity.Login;
import com.anosine.gpsl.android.entity.ModifyPwdInfo;
import com.anosine.gpsl.android.presenter.BasePresenterActivity;
import com.anosine.gpsl.android.presenter.InitPwdPresenter;
import com.anosine.gpsl.android.presenter.LoginPresenter;
import com.anosine.gpsl.android.presenter.ValidatePresenter;
import com.anosine.gpsl.android.ui.InitPwdView;
import com.anosine.gpsl.android.ui.LoginView;
import com.anosine.gpsl.android.ui.ValidateView;
import com.anosine.gpsl.android.util.ActivityFrgManager;
import com.anosine.gpsl.android.util.DeviceUtil;
import com.anosine.gpsl.android.util.StringUtil;
import com.anosine.gpsl.android.util.ToastHelper;
import com.anosine.gpsl.android.util.UserInfoProxy;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * @Title: ValidateOrForgetPwdActivity
 * @Package com.anosine.gpsl.android.ui.activity
 * @Description: 登录界面
 * @author: sunny
 * @date: 2015-11-19
 * @version V1.0
 */
public class ValidateOrForgetPwdActivity extends BasePresenterActivity
		implements ValidateView, InitPwdView {
	String status;
	@ViewInject(R.id.content_tv_title)
	TextView title;
	@ViewInject(R.id.et_login_username)
	EditText mEtUserName;
	@ViewInject(R.id.et_login_realname)
	EditText mEtRealName;
	@ViewInject(R.id.et_login_idno)
	EditText mEtIdNo;
	@ViewInject(R.id.btn_validate_login)
	Button mBt;
	@ViewInject(R.id.describe)
	TextView describe;

	/**
	 * @Title: LoginOnClick(View v)
	 * @Description: 验证登录按钮的点击事件
	 * @param: View v
	 * @return: 无
	 * @throws
	 */
	@OnClick(R.id.btn_validate_login)
	private void LoginOnClick(View v) {
		initActivityOfValidate();
	}

	/**
	 * @Title: BackOnClick(View v)
	 * @Description: 返回按钮的点击事件，点击关闭当前activity
	 * @param: View v
	 * @return: 无
	 * @throws
	 */
	@OnClick(R.id.button_left)
	private void BackOnClick(View v) {
		finish();
	}

	/**
	 * @Title: onCreate(Bundle savedInstanceState)
	 * @Description: 初始化登录页面
	 * @param: savedInstanceState
	 * @return: 无
	 * @throws
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_validate_forgetpwd_layout);
		ViewUtils.inject(this);

		Login info = UserInfoProxy.getInstance().getLoginInfo();
		if (info != null) {
			if (StringUtil.isNotEmpty(info.getUserName())) {
				mEtUserName.setText(info.getUserName());
			}
		}
		Intent intent = getIntent();
		// 获取数据
		status = intent.getStringExtra("status");
		if (status.equals("forget")) {
			title.setText("初始化密码");
			mBt.setText("初始化密码");
			describe.setVisibility(View.GONE);
		} else {
			title.setText("验证登录");
			mBt.setText("验证登录");
			describe.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * @Title: initActivityOfValidate()
	 * @Description: 获取验证登录信息
	 * @param: 无
	 * @return: 无
	 * @throws
	 */
	private void initActivityOfValidate() {
		ValidatePresenter presenter = new ValidatePresenter(this);
		presenter.loadValidate(mEtUserName.getText().toString(),
				DeviceUtil.getDeviceImei(), mEtRealName.getText().toString(),
				GPSLApp.getchannelId(), mEtIdNo.getText().toString(),
				DeviceUtil.getDeviceImsi(), Build.BRAND, "1",
				GPSLApp.getuserId(), Build.MODEL);

	}

	/**
	 * @Title: setValidate(Login t)
	 * @Description: 获取解析完后的验证登录信息
	 * @param: login
	 * @return: 无
	 * @throws
	 */
	@Override
	public void setValidate(Login t) {
		// 登录验证
		if (t != null && "msg_checklogin_suucess".equals(t.message)) {
			if (status.equals("forget")) {
				// 初始化密码
				initActivityOfInitPwd(t.userpass, mEtIdNo.getText().toString(),
						t.userMd5);
				finish();
			} else {
				// 保存数据
				ActivityFrgManager.getInstance().finishActivities();
				t.isLoginAuto = true;
				t.setUserName(UserInfoProxy.getInstance().getLoginInfo()
						.getUserName());
				t.setPassWord(UserInfoProxy.getInstance().getLoginInfo()
						.getPassWord());
				try {
					if (UserInfoProxy.getInstance().getLoginInfo().SaveOid != null) {
						t.SaveOid = UserInfoProxy.getInstance().getLoginInfo().SaveOid;
					}
				} catch (Exception e) {
					
				}				UserInfoProxy.getInstance().setLoginUserInfo(t);
				startActivity(new Intent(getApplication(), GPSLMainActivity.class));
				finish();
			}
		} else {
			if ("msg_fail".equals(t.message)) {
				ToastHelper.toast("你输入的参数错误!");
			} else if ("msg_checklogin_false".equals(t.message)) {
				ToastHelper.toast("验证登录失败!");
			}
		}
	}

	/**
	 * @Title: initActivityOfInitPwd(String userpass, String idno, String
	 *         usermd5)
	 * @Description: 获取修改密码信息
	 * @param: 无
	 * @return: 无
	 * @throws
	 */
	private void initActivityOfInitPwd(String userpass, String idno,
			String usermd5) {
		InitPwdPresenter presenter = new InitPwdPresenter(this);
		presenter.loadInitPwd(usermd5, userpass, idno);
	}

	/**
	 * @Title: setInitPwd(ModifyPwdInfo modifyPwd)
	 * @Description: 获取解析完后的修改密码信息
	 * @param: modifyPwd
	 * @return: 无
	 * @throws
	 */
	@Override
	public void setInitPwd(ModifyPwdInfo t) {
		// message 字符串
		// status ： true 成功 false 失败
		// msg_login_pwdtrue:修改密码成功
		// msg_login_pwdnouser: 新密码不能为空
		// msg_login_error: 参数错误
		// msg_login_userpassflase: 旧密码输入错误
		// msg_login_pwdfalse: 修改密码失败
		if ("msg_login_pwdtrue".equals(t.message)) {
			ToastHelper.toast("修改密码成功!");
		} else if ("msg_login_pwdnouser".equals(t.message)) {
			ToastHelper.toast("新密码不能为空!");
		}
		if ("msg_login_error".equals(t.message)) {
			ToastHelper.toast("参数错误!");
		}
		if ("msg_login_userpassflase".equals(t.message)) {
			ToastHelper.toast("旧密码输入错误!");
		}
		if ("msg_login_pwdfalse".equals(t.message)) {
			ToastHelper.toast("修改密码失败!");
		}
	}
}
